<template>
  <div class="order-container">
    <div class="order-header">
      <h2>订单信息</h2>
    </div>
    
    <div class="order-details">
      <div class="detail-item">
        <label>订单号：</label>
        <span>{{ order.serialNumber }}</span>
      </div>
      
      <div class="detail-item">
        <label>付款用户：</label>
        <span>{{ userName }}</span>
      </div>
      
      <div class="detail-item">
        <label>收货地址：</label>
        <select v-model="selectedAddress" class="address-select">
          <option 
            v-for="(address, index) in addressList" 
            :key="index" 
            :value="address.address"
          >
            {{ address.address }}
          </option>
        </select>
      </div>
      
      <div class="detail-item total-amount">
        <label>总金额：</label>
        <span>{{ order.cost }}</span>
      </div>
    </div>
    
    <div class="order-actions">
      <button class="confirm-btn-v" @click.one="wx_handleConfirm">微信付款</button>
      <button class="confirm-btn-z" @click.one="zf_handleConfirm">支付宝付款</button>
      <button class="cancel-btn" @click="handleCancel">取消</button>
    </div>
  </div>
</template>


<script setup>
import { ref ,onMounted,onUnmounted} from 'vue'
import store from '@/store'
import { useRoute } from 'vue-router'
import axios from 'axios'

const route = useRoute()

const orderId = route.query.orderId;

const order = ref({})
const userName = ref(sessionStorage.getItem('userName'));
const userId = ref(sessionStorage.getItem('id'));
const selectedAddress = ref('')


//得到订单数据
const getOrder = async () => {
  try {
    const response = await axios.get('/api/getOrderById?id='+orderId)
    order.value = response.data
    console.log(order.value);
    selectedAddress.value = order.value.userAddress;
  } catch (error) {
    console.error('获取数据失败:', error)
  }
}


//得到用户的地址数据
const addressList = ref([])
const getAddress = async () => {
  try {
    const response = await axios.get('/api/getUserAddressList?userId='+userId.value)
    addressList.value = response.data.userAddressList

    
  } catch (error) {
    console.error('获取数据失败:', error)
  }
}


// 处理微信付款
const wx_handleConfirm = () => {
  alert('微信确认付款功能将在这里实现')
}

// 处理支付宝付款
const zf_handleConfirm = async () => {
  console.log('zf_handleConfirm');
  
    try{
        const response = await axios.get('api/payOrder', {
        params: {
            serialNumber: order.value.serialNumber
        },
        // responseType: 'text' // 如果需要在浏览器中直接跳转到支付页面，需要设置responseType
        })
        // 使用 Vue 的 ref 或响应式数据来处理返回的内容，而不是直接操作 DOM
        console.log(response.data);
        if(response.data.code==5500){
          alert(response.data.reason)

        }
        
        const paymentFormHtml = response.data;
        // 创建一个临时的 div 元素来解析 HTML 字符串
        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = paymentFormHtml;
        
        // 获取表单元素
        const form = tempDiv.querySelector('form');
        if (form) {
            // 将表单添加到页面中
            document.body.appendChild(form);
            // 提交表单
            form.submit();
        } else {
            console.error('未找到支付表单');
        }
    }catch(error){
        console.error(error)
    }
}


// 处理取消
const handleCancel = () => {
  if (confirm('确定取消支付订单吗？')) {
    history.back()
  }
}

    // 进入管理页面时隐藏HeaderView
onMounted(() => {
    store.commit('setShowPage', 1)
    getOrder()
    getAddress()
})

// 离开管理页面时恢复HeaderView
onUnmounted(() => {
    store.commit('setShowPage', 0)
})


</script>


<style>
.order-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  font-family: Arial, sans-serif;
}

.order-header {
  border-bottom: 1px solid #e0e0e0;
  padding-bottom: 15px;
  margin-bottom: 20px;
}

.order-header h2 {
  margin: 0;
  color: #333;
  font-size: 1.5rem;
}

.order-details {
  margin-bottom: 30px;
}

.detail-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  padding: 10px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.detail-item:hover {
  background-color: #f9f9f9;
}

.detail-item label {
  flex: 0 0 120px;
  font-weight: 600;
  color: #666;
}

.detail-item span {
  color: #333;
  font-size: 1rem;
}

.address-select {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  color: #333;
  background-color: #fff;
  cursor: pointer;
}

.address-select:focus {
  outline: none;
  border-color: #4d90fe;
  box-shadow: 0 0 0 2px rgba(77, 144, 254, 0.2);
}

.total-amount {
  margin-top: 20px;
  padding-top: 15px;
  border-top: 1px dashed #e0e0e0;
}

.total-amount span {
  color: #e53e3e;
  font-size: 1.2rem;
  font-weight: bold;
}

.order-actions {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 20px;
}

.confirm-btn-v, .confirm-btn-z ,.cancel-btn {
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
}

.confirm-btn-v {
  background-color: #28a745;
  color: white;
}

.confirm-btn-v:hover {
  background-color: #218838;
}

.confirm-btn-z {
  background-color: #1573F7;
  color: white;
}
.confirm-btn-z:hover {
  background-color: #1573F7;
}

.cancel-btn {
  background-color: #6c757d;
  color: white;
}

.cancel-btn:hover {
  background-color: #5a6268;
}
</style>
